<template>
    
    <uni-forms ref="formRef" :modelValue="formData" :rules="rules">
        <uni-forms-item label="账号" name="account">
            <uni-easyinput type="text" v-model="formData.account" placeholder="请输入姓名"/>
        </uni-forms-item>
        <uni-forms-item label="密码" name="password">
            <uni-easyinput type="password" v-model="formData.password" placeholder="请输入密码"/>
        </uni-forms-item>
    </uni-forms>
    <button type="primary" @click="qly.form.pass(submit,formRef)">Submit</button>
    <button type="primary" class="login-btn" open-type="getPhoneNumber"
            @getphonenumber="decryptPhoneNumber">本机号码一键登录
    </button>
</template>
<script setup>

const formRef = ref()
const formData = ref({
    account: 'admin1',
    password: '123456'
})
const rules = {
    account: {
        rules: [
            {
                required: true,
                errorMessage: '请填写用户名',
            },
        ],
    },
    password: {
        rules: [
            {
                required: true,
                errorMessage: '请填写密码',
            },
        ],
    }
}

// 登录
const submit = async () => {
    console.log('登录')
    const res = await qly.api.auth.accountLogin(formData.value)
    console.log('res', res)
    if (res.code === 1) {
        const userInfo = qly.storage.storageRef('userInfo')
        userInfo.value = res.data.userinfo
        const token = qly.storage.storageRef('token')
        token.value = res.data.userinfo.token
        qly.dialog.success('登录成功')
        uni.reLaunch({
            url: '/pages/tabBar/home/index'
        })
    } else {
        qly.dialog.apiError(res).catch()
    }
}
const decryptPhoneNumber = async (e) => {
    
    if (e.detail.errMsg != "getPhoneNumber:fail user deny") {
        console.log('e', e)
        uni.login({
            provider: 'weixin', //使用微信登录
            scopes: 'auth_user',
            success: async function (loginRes) {
                const res = await qly.api.auth.weixinLogin({
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    code: loginRes.code,
                })
            },
            fail: function (err) {
                console.log('err', err)
            }
        });
        
    }
}
</script>
<style scoped>

</style>
